package cn.offcn.service.impl;

import cn.offcn.entity.*;
import cn.offcn.mapper.BlogMapper;
import cn.offcn.mapper.BtypeMapper;
import cn.offcn.mapper.EvaluateMapper;
import cn.offcn.service.BtypeService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author CHTt
 * @Date: 2021/04/23/ 下午 08:35
 * @Description
 */
@Service
public class BtypeServiceImpl implements BtypeService {

    @Autowired
    private BtypeMapper btypeMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public List<Btype> getBtypes() {

        BtypeExample btypeExample = new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidIsNull();
        return btypeMapper.selectByExample(btypeExample);
    }

    @Override
    public OAResult addBtype(Btype btype) {

        int rows = btypeMapper.insert(btype);
        if (rows==1){
            return OAResult.ok(200,"添加成功");
        }
        return OAResult.ok(400,"添加失败");
    }

    @Override
    public List<Btype> getSallList(int typeid) {

        BtypeExample btypeExample = new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidEqualTo(typeid);
        return btypeMapper.selectByExample(btypeExample);
    }

    @Override
    public OAResult deleteSubType(int typeid) {

        int rows = btypeMapper.deleteByPrimaryKey(typeid);
        if (rows==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }

    @Override
    public Btype getBtypeById(int typeid) {

        return btypeMapper.selectByPrimaryKey(typeid);
    }

    @Override
    public OAResult updateBtypeById(Btype btype) {
        int rows = btypeMapper.updateByPrimaryKeySelective(btype);
        if (rows==1){
            return OAResult.ok(200,"更新成功");
        }
        return OAResult.ok(400,"更新失败");
    }

    @Override
    public OAResult deleteBigBtype(int typeid) {

        // 删除子类别
        BtypeExample btypeExample = new BtypeExample();
        BtypeExample.Criteria criteria = btypeExample.createCriteria();
        criteria.andTypePidEqualTo(typeid);

        // 子类别集合
        List<Btype> btypeList = btypeMapper.selectByExample(btypeExample);
        for (Btype btype : btypeList) {
            BlogExample blogExample = new BlogExample();
            BlogExample.Criteria blogExampleCriteria  = blogExample.createCriteria();
            blogExampleCriteria.andTypeFkEqualTo(btype.getTypeid());
            List<Blog> blogList = blogMapper.selectByExample(blogExample);
            if (blogList!=null && blogList.size()>0){
                for (Blog blog : blogList) {
                    EvaluateExample evaluateExample = new EvaluateExample();
                    EvaluateExample.Criteria evaluateExampleCriteria = evaluateExample.createCriteria();
                    evaluateExampleCriteria.andBFkEqualTo(blog.getBid());
                    evaluateMapper.deleteByExample(evaluateExample);
                }
            }
            // 删除博客
            blogMapper.deleteByExample(blogExample);
        }
        btypeMapper.deleteByExample(btypeExample);

        // 再删除大类别
        int rows = btypeMapper.deleteByPrimaryKey(typeid);
        if (rows==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }
}
